package org.tabcap;

import java.awt.BorderLayout;
import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;

import javax.swing.JPanel;

import org.tabcap.gui.about.TCAbout;
import org.tabcap.gui.menu.TCButtonToolbar;
import org.tabcap.gui.menu.TCMenuBar;
import org.tabcap.res.ImageCache;
import org.tabcap.workspace.manager.DefaultWorkspaceManager;
import org.tabcap.workspace.manager.TCWorkspaceManager;

public class TCInstance extends JPanel implements WindowListener
{
	private static final long serialVersionUID = -2202487196600945496L;
	
	private Frame frame;
	public Frame frame() { return frame; }
	
	private ImageCache images;
	public ImageCache images() { return images; }
	
	private TCWorkspaceManager workspaces;
	public TCWorkspaceManager workspaces() { return workspaces; }
	
	private TCAbout about;
	
	public TCInstance(Frame frame)
	{
		super(new BorderLayout());
		
		frame.addWindowListener(this);
		images = new ImageCache();
		about = new TCAbout(this);
		about.setLocationRelativeTo(null);
		workspaces = new DefaultWorkspaceManager(this);
		
		JPanel menuPanel = new JPanel(new BorderLayout());
		menuPanel.add(new TCMenuBar(this), BorderLayout.NORTH);
		menuPanel.add(new TCButtonToolbar(this), BorderLayout.SOUTH);
		
		add(menuPanel, BorderLayout.NORTH);
		add(workspaces.view(), BorderLayout.CENTER);
		workspaces.openNew();
	}

	@Override public void windowOpened(WindowEvent e) { }
	@Override public void windowActivated(WindowEvent e) { }
	@Override public void windowDeactivated(WindowEvent e) { }
	@Override public void windowIconified(WindowEvent e) { }
	@Override public void windowDeiconified(WindowEvent e) { }
	@Override public void windowClosing(WindowEvent e) { workspaces.closeAll(); }
	@Override public void windowClosed(WindowEvent e) { about.dispose(); }
	
	// ******* Actions to be used by menus/the toolbar.
	
	public ActionListener newAction() { return new NewAction(); }
	private class NewAction implements ActionListener {
		@Override public void actionPerformed(ActionEvent e) {
			workspaces.openNew();
		} }
	
	public ActionListener openAction() { return new OpenAction(); }
	private class OpenAction implements ActionListener {
		@Override public void actionPerformed(ActionEvent e) {
			workspaces.open();
		} }
	
	public ActionListener saveAction() { return new SaveAction(); }
	private class SaveAction implements ActionListener {
		@Override public void actionPerformed(ActionEvent e) {
			workspaces.save();
		} }
	
	public ActionListener saveAsAction() { return new SaveAsAction(); }
	private class SaveAsAction implements ActionListener {
		@Override public void actionPerformed(ActionEvent e) {
			workspaces.saveAs();
		} }
	
	public ActionListener saveAllAction() { return new SaveAllAction(); }
	private class SaveAllAction implements ActionListener {
		@Override public void actionPerformed(ActionEvent e) {
			workspaces.saveAll();
		} }
	
	public ActionListener closeAction() { return new CloseAction(); }
	private class CloseAction implements ActionListener {
		@Override public void actionPerformed(ActionEvent e) {
			workspaces.closeCurrent();
		} }
	
	public ActionListener closeAllAction() { return new CloseAllAction(); }
	private class CloseAllAction implements ActionListener {
		@Override public void actionPerformed(ActionEvent e) {
			workspaces.closeAll();
		} }
	
	public ActionListener undoAction() { return new UndoAction(); }
	private class UndoAction implements ActionListener {
		@Override public void actionPerformed(ActionEvent e) {
			workspaces.undo();
		} }
	
	public ActionListener redoAction() { return new RedoAction(); }
	private class RedoAction implements ActionListener {
		@Override public void actionPerformed(ActionEvent e) {
			workspaces.redo();
		} }
	
	public ActionListener copyAction() { return new CopyAction(); }
	private class CopyAction implements ActionListener {
		@Override public void actionPerformed(ActionEvent e) {
			workspaces.copy();
		} }
	
	public ActionListener cutAction() { return new CutAction(); }
	private class CutAction implements ActionListener {
		@Override public void actionPerformed(ActionEvent e) {
			workspaces.cut();
		} }
	
	public ActionListener pasteAction() { return new PasteAction(); }
	private class PasteAction implements ActionListener {
		@Override public void actionPerformed(ActionEvent e) {
			workspaces.paste();
		} }
	
	public ActionListener compileAction() { return new CompileAction(); }
	private class CompileAction implements ActionListener {
		@Override public void actionPerformed(ActionEvent e) {
			if(!workspaces.current().document().needsSave())
				workspaces.save();
			workspaces.current().compile();
		} }
	
	public ActionListener runAction() { return new RunAction(); }
	private class RunAction implements ActionListener {
		@Override public void actionPerformed(ActionEvent e) {
			workspaces.current().run();
		} }
	
	public ActionListener helpAction() { return new HelpAction(); }
	private class HelpAction implements ActionListener {
		@Override public void actionPerformed(ActionEvent e) {
			
		} }
	
	public ActionListener webAction() { return new WebAction(); }
	private class WebAction implements ActionListener {
		@Override public void actionPerformed(ActionEvent e) {
			
		} }
	
	public ActionListener aboutAction() { return new AboutAction(); }
	private class AboutAction implements ActionListener {
		@Override public void actionPerformed(ActionEvent e) {
			about.setVisible(true);
		} }
}